import axios from 'axios'
import { Message } from 'element-ui';
import store from '@/store';
import { getToken } from '@/utils/auth';
import router from '@/router';
// 创建一个axios实例
const service = axios.create({
    baseURL: process.env.VUE_APP_BASE_API,
    timeout: 5000
});
// =============================================== token ====================================== //
// let loading; // 定义loading变量
// function startLoading () {
//     // 使用Element loading-start 方法
//     loading = Loading.service({
//         lock: true,
//         // text: '加载中……',
//         spinner: 'dots-loader',
//         background: 'hsla(0, 0%, 100%, .8)'
//     });
// }

// async function endLoading () {
//     await new Promise((resolve, reject) => {
//         // 使用Element loading-close 方法
//         setTimeout(() => {
//             loading.close();
//             resolve()
//         }, 500)
//     })
// }

// request interceptor
service.interceptors.request.use((request) => {
    // startLoading();
    if (store.getters.token) {
        request.headers.Authorization = getToken()
    }
    return request;
}, (error) => {
    // await endLoading();
    return Promise.reject(error)
});

// response interceptor
service.interceptors.response.use(async (response) => {
    // await endLoading();
    const res = response.data;
    if (res instanceof Blob) {
        return res;
    }
    if (res.code !== 200) {
        Message({
            message: res.msg || 'Error',
            type: 'error',
            duration: 5 * 1000
        })
        // 50008: 非法令牌; 50012: 已其他客户端登录; 50014: 令牌已过期;
        if (res.code === 401) {
            store.dispatch('user/resetToken').then(() => {
                location.reload()
            })
        }
        return Promise.resolve(new Error(res.msg || 'Error'))
    } else {
        return res
    }
}, async (error) => {
    // await endLoading();
    console.log('err', error); // for debug
    if (error && error.response) {
        switch (error.response.status) {
        case 400:
            error.message = '请求错误(400)';
            break;
        case 401:
            return router.push('/login');
        case 403:
            error.message = '拒绝访问(403)';
            break;
        case 404:
            error.message = '请求出错(404)';
            break;
        case 408:
            error.message = '请求超时(408)';
            break;
        case 500:
            error.message = '服务器错误(500)';
            break;
        case 501:
            error.message = '服务未实现(501)';
            break;
        case 502:
            error.message = '网络错误(502)';
            break;
        case 503:
            error.message = '服务不可用(503)';
            break;
        case 504:
            error.message = '网络超时(504)';
            break;
        case 505:
            error.message = 'HTTP版本不受支持(505)';
            break;
        default:
            error.message = `连接出错(${error.response.status})!`;
        }
        Message({
            message: error.message,
            type: 'error',
            duration: 3 * 1000
        });
    }
    return Promise.reject(error);
});
export default service;
